home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Light / drawI.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-20  |  7.3 KB  |  264 lines

  1. /**********************************************************************/
  2. /* drawI.c :                                                          */
  3. /*                                                                    */
  4. /* Scene drawing, and hemicube computations on SGI framebuffer        */
  5. /*                                                                    */
  6. /* Copyright (C) 1992, Bernard Kwok                                   */
  7. /* All rights reserved.                                               */
  8. /* Revision 1.0                                                       */
  9. /* May, 1992                                                          */
  10. /**********************************************************************/
  11. #include <stdio.h>
  12. #include <gl/gl.h>
  13. #define IRIS4D
  14. #include "geo.h"
  15. #include "io.h"
  16. #include "struct.h"
  17. #include "rad.h"
  18. #include "vcr.h"
  19.  
  20. extern RadParams ReadLog;
  21. extern Hemicube hemicube;
  22. extern OptionType Option;
  23. Matrix Identity = {
  24.   1,0,0,0,
  25.   0,1,0,0,
  26.   0,0,1,0,
  27.   0,0,0,1
  28. };
  29.  
  30. int showmesh = 0;
  31. long HCWid;                   /* window to use of hc */
  32. long DisplWid;                /* window to use for display */
  33. /* long CurWid;                  for debugging only */
  34. float zmax;
  35.  
  36. /**********************************************************************/
  37. /* Function prototypes                                                */
  38. /**********************************************************************/
  39. void Reset_Buffers();
  40. void Begin_DrawHC();
  41. void End_DrawHC();
  42. void Draw_Polygon();
  43. void Draw_PolygonRGB();
  44. void Begin_DrawDispl();
  45. void End_DrawDispl();
  46. void Create_Display_Buffer();
  47. void Create_Item_Buffer();
  48. void CleanUpBuffers();
  49.  
  50. /**********************************************************************/
  51. /* Clear z and frame buffers                                          */
  52. /**********************************************************************/
  53. void Reset_Buffers(fvalue, zvalue)
  54.      unsigned long fvalue;
  55.      float zvalue;
  56. {  czclear(fvalue, zvalue); }
  57.  
  58. /**********************************************************************/
  59. /* Set up item buffer, and view */
  60. /**********************************************************************/
  61. void Begin_DrawHC(view, background)
  62.      Camera view;
  63.      unsigned long background;
  64. {
  65.   
  66.   winset(HCWid);
  67.   /* if (Option.debug) {
  68.      CurWid = winget();
  69.      printf("+ Current window: %d\n", CurWid);
  70.      printf("+ Eye: view.fovy = %d, near = %g, far = %g\n",
  71.      view.fovy, view.near, view.far);
  72.      printf("  lookfrom: %g,%g,%g; lookat: %g,%g,%g\n",
  73.      view.lookfrom.x, view.lookfrom.y, view.lookfrom.z, 
  74.      view.lookat.x, view.lookat.y, view.lookat.z);
  75.      printf("  bank: %g\n", view.bank);
  76.      }
  77.      */
  78.  
  79.   pushmatrix();
  80.   perspective((int)view.fovy*10,1,view.near, view.far);
  81.   lookat(view.lookfrom.x, view.lookfrom.y, view.lookfrom.z,
  82.      view.lookat.x, view.lookat.y, view.lookat.z, 0);
  83.   Reset_Buffers(background,zmax);
  84. }
  85.  
  86. /**********************************************************************/
  87. /* Read the frame buffer to hemicube buffer */
  88. /**********************************************************************/
  89. void End_DrawHC()
  90. {
  91.   long pixels_read;
  92.   unsigned long temp[13000];  /* Fixed size for now... */
  93.   unsigned long *pb;
  94.   int i;
  95.   int hsize = ReadLog.hemicubeRes;
  96.  
  97.   winset(HCWid);
  98.   popmatrix();
  99.   for (i=0;i<13000;i++) temp[i] = kBackgroundItem;
  100.  
  101.   readsource(SRC_FRONT);
  102.   pixels_read = lrectread(1,1,hemicube.view.xRes,hemicube.view.yRes,temp);
  103.   pb = hemicube.view.buffer;
  104.   for(i=0;i<(hsize*hsize);i++, pb++) 
  105.     *pb = temp[i];  
  106. }
  107.  
  108. /**********************************************************************/
  109. /* Draw patch */
  110. /**********************************************************************/
  111. void Draw_Polygon(nPts,pts, normal, p_colour)
  112.      int nPts;
  113.      Vector pts[MAX_PATCH_VTX];
  114.      Vector *normal;
  115.      unsigned long p_colour;
  116. {
  117.   int i;
  118.   float fpoint[3];
  119.  
  120.   shademodel(FLAT);
  121.   cpack(p_colour);
  122.  
  123.   bgnpolygon();
  124.   for(i=0;i<nPts;i++) {
  125.     fpoint[0] = pts[i].x; fpoint[1] = pts[i].y; fpoint[2] = pts[i].z;
  126.     v3f(fpoint); 
  127.   }
  128.   endpolygon();
  129. }
  130.  
  131. /**********************************************************************/
  132. /* Draw polygons in RGB */
  133. /**********************************************************************/
  134. void Draw_PolygonRGB(nPts,pts, normal, p_colour)
  135.      int nPts;
  136.      Vector pts[MAX_PATCH_VTX];
  137.      Vector *normal;
  138.      Colour p_colour[4];
  139. {
  140.   int i;
  141.   float fpoint[3];
  142.   float fnorm[3];
  143.   float fcolour[3];
  144.  
  145.   fnorm[0] = normal->x; fnorm[1] = normal->y; fnorm[2] = normal->z;
  146.  
  147.   shademodel(GOURAUD);
  148.   
  149.   if (showmesh)
  150.     bgnline();
  151.   else
  152.     bgnpolygon(); 
  153.  
  154.   for(i=0;i<nPts;i++) {
  155.     fpoint[0] = pts[i].x; 
  156.     fpoint[1] = pts[i].y; 
  157.     fpoint[2] = pts[i].z;
  158.     fcolour[0] = p_colour[i].r; 
  159.     fcolour[1] = p_colour[i].g; 
  160.     fcolour[2] = p_colour[i].b;
  161.  
  162.     c3f(fcolour);
  163.     /* n3f(fnorm); */
  164.     v3f(fpoint); 
  165.   }
  166.  
  167.   if (showmesh)
  168.     endline();
  169.   else
  170.     endpolygon();
  171. }
  172.  
  173. /**********************************************************************/
  174. /* Set camera, and clear window to draw in                            */
  175. /**********************************************************************/
  176. void Begin_DrawDispl(view, background)
  177.      Camera view;
  178.      unsigned char background;
  179. {
  180.   winset(DisplWid);
  181.   pushmatrix();
  182.   perspective(view.fovy*10,1,view.near, view.far);
  183.   lookat(view.lookfrom.x, view.lookfrom.y, view.lookfrom.z,
  184.      view.lookat.x, view.lookat.y, view.lookat.z, view.bank);
  185.   Reset_Buffers(0x000000,zmax);
  186. }
  187.  
  188. /**********************************************************************/
  189. /* Swap buffers                                                       */ 
  190. /**********************************************************************/
  191. void End_DrawDispl()
  192. {
  193.   winset(DisplWid); 
  194.   popmatrix();
  195.   swapbuffers();
  196. }
  197.  
  198. /**********************************************************************/
  199. /* Create buffer to display scene */
  200. /**********************************************************************/
  201. void Create_Display_Buffer(sizex, sizey)
  202.      int sizex, sizey;
  203. {
  204.   if (Option.UseVCR == TRUE) {
  205.     vcr_set_mode(Option.VCRmode);
  206.     vcr_setup_window(0);
  207.     DisplWid = winopen("");
  208.   } else {
  209.     prefsize(sizex,sizey);
  210.     DisplWid = winopen("PR Radiosity");
  211.   }
  212.   
  213.   zbuffer(TRUE);
  214.   zmax = getgdesc(GD_ZMAX);
  215.   doublebuffer();
  216.   RGBmode();
  217.   mmode(MVIEWING);
  218.   loadmatrix(Identity);
  219.   Reset_Buffers(0xff000f, zmax);
  220.   gconfig();
  221.  
  222.   if (Option.UseVCR == TRUE) {
  223.     vcr_start_video();
  224.   }
  225. }
  226.  
  227. /**********************************************************************/
  228. /* Set up zbuffer, and item_buffer (part of frame-buffer)             */
  229. /**********************************************************************/
  230. void Create_Item_Buffer(buff_sizex, buff_sizey)
  231.      int buff_sizex, buff_sizey;
  232. {
  233.   keepaspect(1,1);
  234.  
  235.   /* Setup frame buffer area of size buff_size as item buffer */
  236.   if (Option.UseVCR == TRUE) {
  237.     prefposition(0,buff_sizex-1,0,buff_sizey-1);
  238.     prefsize(buff_sizex,buff_sizey);
  239.   } else {
  240.     prefsize(buff_sizex,buff_sizey);
  241.     HCWid = winopen("FF");
  242.   }
  243.   zmax = getgdesc(GD_ZMAX);
  244.   RGBmode();
  245.   mmode(MVIEWING);
  246.     zbuffer(TRUE);
  247.   Reset_Buffers(kBackgroundItem,zmax);
  248.   readsource(SRC_AUTO);
  249.   loadmatrix(Identity);
  250.   gconfig();
  251. }
  252.  
  253.  
  254. /**********************************************************************/
  255. /* Close windows */
  256. /**********************************************************************/
  257. void CleanUpBuffers()
  258. {
  259.   sleep(5);
  260.   if (Option.ff_raytrace == 0)
  261.     winclose(HCWid);
  262.   winclose(DisplWid);
  263. }
  264.